<?php

/**
 * 模型对应数据库表 "web_customers".
 *
 * 以下是可用表web_customers的栏位:
 * @property string $id
 * @property string $site_key
 * @property string $mail
 * @property string $password
 * @property string $salt
 * @property string $member_info
 * @property string $fast_name
 * @property string $last_name
 * @property string $country
 * @property string $city
 * @property string $address
 * @property string $zip_code
 * @property string $phone
 * @property string $msn
 * @property string $paypal_mail
 * @property string $total_credits
 * @property string $member_level
 * @property string $monetary
 * @property integer $status
 */
class WebCustomers extends CActiveRecord {

    /**
     * 返回指定的基于“ar”的静态模型类.
     * @return WebCustomers the static model class
     * @author nirsh
     * @example
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回数据库表的名.
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function tableName() {
        return 'web_customers';
    }

    /**
     * 返回验证规则
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function rules() {
        return array(
            array('site_key,mail,password', 'required'),
            array('status', 'numerical', 'integerOnly' => true),
            array('salt', 'length', 'max' => 16),
            array('site_key', 'length', 'max' => 100),
            array('mail, user_name,address, msn, paypal_mail', 'length', 'max' => 255),
            array('password', 'length', 'max' => 32),
            array('fast_name, last_name, country, city, phone', 'length', 'max' => 50),
            array('zip_code, total_credits, monetary', 'length', 'max' => 10),
            array('member_level', 'length', 'max' => 3),
            array('mail', 'email'),
            array('member_info,re_time', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, site_key, mail, password, salt, member_info, fast_name, last_name, country, city, address, zip_code, phone, msn, paypal_mail, total_credits, member_level, monetary, status,re_time', 'safe', 'on' => 'search'),
        );
    }

    /**
     * 用户验证
     * @param string $mail
     * @param string $pass
     * @param string $site_key 网站KEY
     * @return boolean 0代表用户不存在, FALSE代表密码错误
     */
    public function authenticationCustomer($mail, $pass) {
        $site_key = Yii::app()->params->domain;
        $user = $this->findByAttributes(array('mail' => $mail, 'site_key' => $site_key));
        if ($user) {
            $pass = $this->generationPassword($pass, $user->salt);
            if ($pass == $user->password) {
                return $user->attributes;
            } else {
                return FALSE;
            }
        } else {
            return 0;
        }
    }

    /**
     * 生成密码
     * @param string $pass 明文密码
     * @param string $salt 加密串
     * @return string 
     */
    public function generationPassword($pass, $salt) {
        return md5($salt . $pass);
    }

    /**
     * 生Salt
     * @return string 
     */
    public function generationSalt() {
        $str = md5(microtime());
        return substr($str, -6);
    }

    /**
     * 返回关联规则数组。
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array();
    }

    /**
     * 通过ID获取名称
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function getNameById($id, $nameField) {
        $res = $this->findByPk($id);
        return $res[$nameField] ? $res[$nameField] : '';
    }

    /**
     * 返回属性标签(名称= >标签)数组
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'site_key' => '网站',
            'mail' => '邮箱',
            'password' => '密码',
            'salt' => '密码干扰串',
            'member_info' => '会员说明',
            'fast_name' => '姓',
            'last_name' => '名',
            'country' => '国家',
            'city' => '城市',
            'address' => '地址',
            'zip_code' => '邮政编码',
            'phone' => '手机号码',
            'msn' => 'MSN',
            'paypal_mail' => 'PayPal邮箱',
            'total_credits' => '积分',
            'member_level' => '会员等级',
            'monetary' => '消费金额',
            'status' => '状态',
            're_time' => '注册时间',
        );
    }

    /**
     * 会员等级列表 
     */
    public $member_level_array = array(
        2 => 'VIP',
        3 => 'Ordinary',
    );

    /*
     * 状态
     */
    public $status_array = array(
        0 => '禁止',
        1 => '激活',
    );

    /**
     * 设置查询相关条件
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;
        $criteria->compare('id', $this->id);
        $criteria->compare('site_key', $this->site_key);
        $criteria->compare('mail', $this->mail);
        $criteria->compare('password', $this->password);
        $criteria->compare('salt', $this->salt);
        $criteria->compare('fast_name', $this->fast_name, true);
        $criteria->compare('last_name', $this->last_name, true);
        $criteria->compare('country', $this->country, true);
        $criteria->compare('city', $this->city, true);
        $criteria->compare('address', $this->address, true);
        $criteria->compare('zip_code', $this->zip_code, true);
        $criteria->compare('phone', $this->phone, true);
        $criteria->compare('msn', $this->msn, true);
        $criteria->compare('paypal_mail', $this->paypal_mail, true);
        $criteria->compare('total_credits', $this->total_credits, true);
        $criteria->compare('member_level', $this->member_level, true);
        $criteria->compare('monetary', $this->monetary, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('re_time', $this->re_time, true);
        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

    /**
      +-----------------------------------------------------
     * 说明 通过email获取用户 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     * 
     */
    public function getCustome($mail) {
        $cdbc = new CDbCriteria;
        $cdbc->compare('site_key', Yii::app()->params->domain);
        $cdbc->compare('mail', $mail);
        return $this->find($cdbc);
    }

    /**
     * 修改密码
     * @param int $uid
     * @param string $oldPass
     * @param string $newPass
     * @return boolean|string 
     */
    public function CustomersUpdatePass($uid, $oldPass, $newPass) {
        $member = $this->findByPk($uid);
        if ($member) {
            if ($oldPass == '' || $this->generationPassword($oldPass, $member->salt) == $member->password) {
                $newPass = empty($newPass) ? $member->password : $this->generationPassword($newPass, $member->salt);
                $member->password = $newPass;
                if ($member->save()) {
                    return 'Change Success!';
                } else {
                    return 'Update field!';
                }
            } else {
                return 'Password error!';
            }
        } else {
            return 'Not find Member!';
        }
    }

    /**
     * 查看用户是否存在
     * @param string $mail
     * @return bool|array 
     */
    public function checkMemberByMail($mail) {
        $cdbc = new CDbCriteria;
        $cdbc->compare('mail', $mail);
        $cdbc->compare('site_key', Yii::app()->params->domain);
        $member = $this->find($cdbc);
        return $member;
    }

}
